package LeetCode.month02.day35;

import java.util.ArrayList;
import java.util.Arrays;

public class No134 {
    public static void main(String[] args) {
        System.out.println(canCompleteCircuit(new int[]{2,3,4},new int[]{3,4,3}));
    }
    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int[] arr = new int[2*n];
        for (int i = 0; i < n; i++) {
            arr[i] = gas[i]-cost[i];
            arr[i+n]=arr[i];
        }
        long sum = 0;
        int left = 0;
        for (int i = 0;; i++) {
            sum+=arr[i];
            if(sum<0){
                sum=0;
                left = i+1;
            }
            if(i-left==n-1) return left;
            if(i==2*n-1) return -1;
        }
    }
}
